package com.chenyaowei.member.service.impl;

import com.chenyaowei.common.utils.SecurityUtils;
import com.chenyaowei.member.domain.CywMemberReceiveAddress;
import com.chenyaowei.member.mapper.CywMemberReceiveAddressMapper;
import com.chenyaowei.member.service.ICywMemberReceiveAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 会员收货地址Service业务层处理
 *
 * @author 陈耀威
 * @date 2023-12-03
 */
@Service
public class CywMemberReceiveAddressServiceImpl implements ICywMemberReceiveAddressService {
    @Autowired
    private CywMemberReceiveAddressMapper cywMemberReceiveAddressMapper;

    /**
     * 查询会员收货地址
     *
     * @param id 会员收货地址主键
     * @return 会员收货地址
     */
    @Override
    public CywMemberReceiveAddress selectCywMemberReceiveAddressById(Long id) {
        return cywMemberReceiveAddressMapper.selectCywMemberReceiveAddressById(id);
    }

    /**
     * 查询会员收货地址列表
     *
     * @param cywMemberReceiveAddress 会员收货地址
     * @return 会员收货地址
     */
    @Override
    public List<CywMemberReceiveAddress> selectCywMemberReceiveAddressList(CywMemberReceiveAddress cywMemberReceiveAddress) {
        return cywMemberReceiveAddressMapper.selectCywMemberReceiveAddressList(cywMemberReceiveAddress);
    }

    /**
     * 新增会员收货地址
     *
     * @param cywMemberReceiveAddress 会员收货地址
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertCywMemberReceiveAddress(CywMemberReceiveAddress cywMemberReceiveAddress) {
        if (cywMemberReceiveAddressMapper.selectMemberAddressCount(SecurityUtils.getUserId()) < 20) {
            resetDefaultAddress(cywMemberReceiveAddress.getDefaultStatus());
            return cywMemberReceiveAddressMapper.insertCywMemberReceiveAddress(cywMemberReceiveAddress);
        }
        return 0;

    }

    private int resetDefaultAddress(Integer defaultStatus) {
        if (defaultStatus == 1) {
            return cywMemberReceiveAddressMapper.resetDefaultAddress(SecurityUtils.getUserId());
        }
        return 0;
    }

    /**
     * 修改会员收货地址
     *
     * @param cywMemberReceiveAddress 会员收货地址
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCywMemberReceiveAddress(CywMemberReceiveAddress cywMemberReceiveAddress) {
        resetDefaultAddress(cywMemberReceiveAddress.getDefaultStatus());
        return cywMemberReceiveAddressMapper.updateCywMemberReceiveAddress(cywMemberReceiveAddress);
    }

    /**
     * 批量删除会员收货地址
     *
     * @param ids 需要删除的会员收货地址主键
     * @return 结果
     */
    @Override
    public int deleteCywMemberReceiveAddressByIds(Long[] ids) {
        return cywMemberReceiveAddressMapper.deleteCywMemberReceiveAddressByIds(ids);
    }

    /**
     * 删除会员收货地址信息
     *
     * @param id 会员收货地址主键
     * @return 结果
     */
    @Override
    public int deleteCywMemberReceiveAddressById(Long id) {
        return cywMemberReceiveAddressMapper.deleteCywMemberReceiveAddressById(id);
    }

    @Override
    public List<CywMemberReceiveAddress> selectListByUserId(Long userId) {
        return cywMemberReceiveAddressMapper.selectListByUserId(userId);
    }
}
